import {
  applyDecorators,
  MethodNotAllowedException,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';

export const upload = (fileName = 'file', options: MulterOptions = {}) => {
  return applyDecorators(UseInterceptors(FileInterceptor(fileName, options)));
};

export const fileFilter = (...types: string[]) => {
  return (req, file, cb) => {
    if (types.some((item) => file.mimetype.includes(item))) {
      return cb(null, true);
    }
    cb(new MethodNotAllowedException('类型不支持'), false);
  };
};

// 图片上传
export const image = (filed = 'file') => {
  return upload(filed, {
    fileFilter: fileFilter('image'),
  });
};
